{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2a29a388",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "> 0. 代码的注释\n",
    ">> 1. #\n",
    ">> 2. ''' '''\n",
    "> 1. 用户输入\n",
    ">> input()\n",
    "> 2. 格式化输出\n",
    ">> 1. 字符串拼接\n",
    ">> 2. 占位符 %s\n",
    ">> 3. format()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1f85ec7",
   "metadata": {},
   "source": [
    "## 0.代码的注释\n",
    "\n",
    "> 1. #，一般写在代码块的最上边，或者每一行的最后，用于代码思路的记录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7f64cb50",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Lifa"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4731dc27",
   "metadata": {},
   "source": [
    "> 2.'''注释内容''',一般是注释代码块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c8850145",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "三引号注释代码块内容，不运行\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "import random\n",
    "wait_time = random.randint(1,60)\n",
    "print(wait_time)\n",
    "\n",
    "word = \"bottles\"\n",
    "print(word)\n",
    "'''\n",
    "\n",
    "print(\"三引号注释代码块内容，不运行\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2b1944a",
   "metadata": {},
   "source": [
    "## 1.用户输入\n",
    "\n",
    "> 1. input()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c93f6224",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:notebook\n",
      "密码：123456\n",
      "notebook\n",
      "123456\n"
     ]
    }
   ],
   "source": [
    "# 登录系统\n",
    "username  = input(\"账号:\")\n",
    "password = input(\"密码：\")\n",
    "\n",
    "print(username)\n",
    "print(password)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "08c907e3",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号：notebook\n",
      "密码：1234578\n",
      "账号或者密码错误\n"
     ]
    }
   ],
   "source": [
    "# 登录系统 完善\n",
    "# 假设我们数据库有用户信息：账号：notebook 密码：123456\n",
    "# 方法一\n",
    "user1 = \"notebook\"\n",
    "password1 = \"123456\"\n",
    "\n",
    "username = input(\"账号：\")\n",
    "password = input(\"密码：\")\n",
    "if username == user1:\n",
    "    if password == password1:\n",
    "        print(\"登陆成功\")\n",
    "    else:\n",
    "        print(\"账号或者密码错误\")\n",
    "else:\n",
    "    print(\"账号或者密码错误\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f71c7948",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号：notebook\n",
      "密码：123456\n",
      "欢迎notebook登录\n"
     ]
    }
   ],
   "source": [
    "# 方法二\n",
    "user1 = \"notebook\"\n",
    "password1 = \"123456\"\n",
    "\n",
    "username = input(\"账号：\")\n",
    "password = input(\"密码：\")\n",
    "# and 代表和\n",
    "if user1 == username and password1 == password:\n",
    "    print(\"欢迎\"+userl+\"登录\")\n",
    "else:\n",
    "    print(\"账号不存在或者密码错误\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2b3b3d13",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号：MOTEBOOK\n",
      "密码：FDFD\n",
      "账号不存在或者密码错误\n",
      "您还有2次机会尝试\n",
      "账号：DKKFKKF\n",
      "密码：3\n",
      "账号不存在或者密码错误\n",
      "您还有1次机会尝试\n",
      "账号：33432\n",
      "密码：333\n",
      "三次输入失败，请1小时后再来尝试\n"
     ]
    }
   ],
   "source": [
    "# 登录系统  增加循环功能\n",
    "# 假设我们数据库有用户信息：账号：notebook 密码：123456\n",
    "user1 = \"notebook\"\n",
    "password1 = \"123456\"\n",
    "\n",
    "for i in range(3,0,-1): #i是一个int\n",
    "    username = input(\"账号：\")\n",
    "    password = input(\"密码：\")\n",
    "\n",
    "\n",
    "    if user1 == username and password1 == password:\n",
    "        print(\"欢迎\"+userl+\"登录\")\n",
    "# break 直接结束循环\n",
    "        break\n",
    "    else:\n",
    "        if i == 1:\n",
    "            print(\"三次输入失败，请1小时后再来尝试\")\n",
    "        else:\n",
    "            print(\"账号不存在或者密码错误\")\n",
    "            print(\"您还有\"+str(i-1)+\"次机会尝试\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4eb9f99",
   "metadata": {},
   "source": [
    "# 2.格式化输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "d396c563",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名：jaja\n",
      "年龄：17\n",
      "工作：IT\n",
      "收入：20000\n",
      "地区：广州\n",
      "-----INFO OF jaja------\n",
      "年龄：17\n",
      "工作：IT\n",
      "收入：20000\n",
      "地区：广州\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统  方法一：字符串拼接\n",
    "name = input(\"姓名：\")\n",
    "age = input(\"年龄：\")\n",
    "job = input(\"工作：\")\n",
    "salary = input(\"收入：\")\n",
    "reg = input(\"地区：\")\n",
    "\n",
    "info = '''-----INFO OF '''+ name +'''------'''+'''\n",
    "年龄：'''+age+'''\n",
    "工作：'''+job+'''\n",
    "收入：'''+salary+'''\n",
    "地区：'''+reg\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "19e0e30f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "年龄：11\n",
      "11 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "# input 作为用户输入，其都以字符串形式返回\n",
    "age = input(\"年龄：\")\n",
    "print(age,type(age))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "cd54d4d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名：jack\n",
      "年龄：20\n",
      "工作：IT\n",
      "收入：200000\n",
      "地区：广州\n",
      "-----INFO OF jack------\n",
      "年龄：20\n",
      "工作：IT\n",
      "收入：200000\n",
      "地区：广州 \n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统  方法二：占位符%s str，依次有序输出\n",
    "name = input(\"姓名：\")\n",
    "age = input(\"年龄：\")\n",
    "job = input(\"工作：\")\n",
    "salary = input(\"收入：\")\n",
    "reg = input(\"地区：\")\n",
    "\n",
    "info = '''-----INFO OF %s------\n",
    "年龄：%s\n",
    "工作：%s\n",
    "收入：%s\n",
    "地区：%s '' '%(name,age,job,salary,reg)\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1757dda1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名：小明\n",
      "年龄：22\n",
      "工作：教育\n",
      "收入：20000\n",
      "地区：广州\n",
      "-----INFO OF 小明------\n",
      "年龄：22\n",
      "工作：教育\n",
      "收入：20000\n",
      "地区：广州\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统  方法三：format()  格式化输出\n",
    "name = input(\"姓名：\")\n",
    "age = input(\"年龄：\")\n",
    "job = input(\"工作：\")\n",
    "salary = input(\"收入：\")\n",
    "reg = input(\"地区：\")\n",
    "\n",
    "info = '''-----INFO OF {姓名}------\n",
    "年龄：{年龄}\n",
    "工作：{工作}\n",
    "收入：{收入}\n",
    "地区：{地区}'''.format(年龄 = age,姓名 = name,工作 = job,地区 = reg,收入 = salary)\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "2d6ff319",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名：java\n",
      "年龄：11\n",
      "工作：IT\n",
      "收入：111234\n",
      "地区：广州\n",
      "-----INFO OF java------\n",
      "姓名：java\n",
      "年龄：11\n",
      "工作：IT\n",
      "收入：111234\n",
      "地区：广州\n"
     ]
    }
   ],
   "source": [
    "name = input(\"姓名：\")\n",
    "age = int(input(\"年龄：\")) # 强制将 str 转换成 int\n",
    "job = input(\"工作：\")\n",
    "salary = input(\"收入：\")\n",
    "reg = input(\"地区：\")\n",
    "\n",
    "info = '''-----INFO OF {姓名}------\n",
    "姓名：{姓名}\n",
    "年龄：{年龄}\n",
    "工作：{工作}\n",
    "收入：{收入}\n",
    "地区：{地区}'''.format(年龄 = age,姓名 = name,工作 = job,收入 = salary, 地区 = reg)\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "db10f5c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名：小明\n",
      "年龄：99\n",
      "工作：IT\n",
      "收入：3000000\n",
      "地区：美国\n",
      "-----INFO OF 小明------\n",
      "年龄：99\n",
      "工作：IT\n",
      "收入：3000000\n",
      "地区：美国\n"
     ]
    }
   ],
   "source": [
    "name = input(\"姓名：\")\n",
    "age = input(\"年龄：\")\n",
    "job = input(\"工作：\")\n",
    "salary = input(\"收入：\")\n",
    "reg = input(\"地区：\")\n",
    "\n",
    "info = f'''-----INFO OF {name}------\n",
    "年龄：{age}\n",
    "工作：{job}\n",
    "收入：{salary}\n",
    "地区：{reg}'''\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a6588a8",
   "metadata": {},
   "source": [
    "## 课后练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f61000a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户\n",
    "# 让用户输入商品编号进行商品的购买 input\n",
    "# 用户选择商品后，检查用户余额是否够，若够则直接扣款，不够则提醒用户 if\n",
    "# 用户可以随时退出购买，退出时打印用户已购买的商品和支付宝余额 input"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4ea84380",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的账号密码\n",
      "账号：2541173560\n",
      "密码：abc123\n",
      "登录成功，欢迎回来\n",
      "请输入您的支付宝余额：11\n",
      "支付宝余额为：11\n",
      "退出购买请输入R\n",
      "-----商品------\n",
      "西红柿: 2元  编号:a\n",
      "西瓜：  3元  编号:b\n",
      "矿泉水：3元  编号:c\n",
      "瓜子：  2元  编号:d\n",
      "请输入你要购买的商品的编号：a\n",
      "扣除2元\n",
      "请输入你要购买的商品的编号：b\n",
      "扣除3元\n",
      "请输入你要购买的商品的编号：b\n",
      "扣除3元\n",
      "请输入你要购买的商品的编号：h\n",
      "商品不存在，请输入正确的商品编号\n",
      "请输入你要购买的商品的编号：d\n",
      "扣除2元\n",
      "请输入你要购买的商品的编号：d\n",
      "您的余额不足，无法购买\n",
      "请输入你要购买的商品的编号：d\n",
      "您的余额不足，无法购买\n",
      "请输入你要购买的商品的编号：R\n",
      "退出购买\n",
      "\n",
      "                购买的商品：西红柿\n",
      "                            西瓜\n",
      "                支付宝余额：1\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 尝试一\n",
    "# 支付宝登录 假设支付宝账号和密码\n",
    "age_1 = \"2541173560\"\n",
    "age_2 = \"abc123\"\n",
    "# 商品编号\n",
    "# 给商品编号\n",
    "a = \"西红柿\"\n",
    "b = \"西瓜\"\n",
    "c = \"矿泉水\"\n",
    "d = \"瓜子\"\n",
    "# 打印商品列表给用户\n",
    "商品列表 = '''-----商品------\n",
    "%s: 2元  编号:a\n",
    "%s：  3元  编号:b\n",
    "%s：3元  编号:c\n",
    "%s：  2元  编号:d''' %(a,b,c,d)\n",
    "print(\"请输入您的账号密码\")\n",
    "# 输入账号密码\n",
    "for i in range(3,0,-1):\n",
    "    age_11 = input(\"账号：\")\n",
    "    age_22 = input(\"密码：\")\n",
    "    if age_11 == age_1 and age_22 == age_2:\n",
    "        print(\"登录成功，欢迎回来\")\n",
    "        age_3 = (input(\"请输入您的支付宝余额：\")) # 支付宝余额\n",
    "        print(\"支付宝余额为：\"+age_3)\n",
    "        print(\"退出购买请输入R\")\n",
    "        print(商品列表)\n",
    "        while True:\n",
    "            编号 = input(\"请输入你要购买的商品的编号：\")\n",
    "            if 编号 == \"a\":\n",
    "                a_1 = 2 # 给西红柿价格赋值\n",
    "                if int(age_3) - a_1 >= 0:\n",
    "                    print(\"扣除\"+str(a_1)+\"元\")\n",
    "                    age_3 = int(age_3) - a_1\n",
    "                    购买商品_1 = \"西红柿\"\n",
    "                else:\n",
    "                    print(\"您的余额不足，无法购买\")\n",
    "            elif 编号 == \"b\":\n",
    "                b_1 = 3\n",
    "                if int(age_3) - b_1 >= 0:\n",
    "                    print(\"扣除\"+str(b_1)+\"元\")\n",
    "                    age_3 = int(age_3) - b_1\n",
    "                    购买商品_2 = \"西瓜\"\n",
    "                else:\n",
    "                    print(\"您的余额不足，无法购买\")\n",
    "            elif 编号 == \"c\":\n",
    "                c_1 = 3\n",
    "                if int(age_3) - c_1 >= 0:\n",
    "                    print(\"扣除\"+str(c_1)+\"元\")\n",
    "                    age_3 = int(age_3) - c_1\n",
    "                    购买商品_3 = \"矿泉水\"\n",
    "                else:\n",
    "                    print(\"您的余额不足，无法购买\")\n",
    "            elif 编号 == \"d\":\n",
    "                d_1 = 2\n",
    "                if int(age_3) - d_1 >= 0:\n",
    "                    print(\"扣除\"+str(d_1)+\"元\")\n",
    "                    age_3 = int(age_3) - d_1\n",
    "                    购买商品_4 = \"瓜子\"\n",
    "                else:\n",
    "                    print(\"您的余额不足，无法购买\")\n",
    "            elif 编号 == \"R\":\n",
    "                print(\"感谢您的购买，欢迎下次光临\")\n",
    "                info = '''\n",
    "                购买的商品：%s\n",
    "                            %s\n",
    "                支付宝余额：%s'''%(购买商品_1,购买商品_2,age_3)# 问题：打印商品有误\n",
    "                print(info)\n",
    "                break\n",
    "            else:\n",
    "                print(\"商品不存在，请输入正确的商品编号\")\n",
    "        \n",
    "        break\n",
    "    else:\n",
    "        if i == 1:\n",
    "            print(\"三次输入失败，请1小时后重新尝试\")\n",
    "        else:\n",
    "            print(\"账号不存在或密码错误\")\n",
    "            print(\"您还有\"+str(i-1)+\"次机会，请重新输入\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "23077c7b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：33\n",
      "----------商品列表----------\n",
      "商品序号 (0)： 蜜雪柠檬水 6元\n",
      "商品序号 (1)： 蜜桃四季春 10元\n",
      "商品序号 (2)： 芒果奶盖 12元\n",
      "商品序号 (3)： 葡萄波波奶茶 8元\n",
      "请输入商品编号：3\n",
      "商品购买成功，正在制作中...您的余额还有25元\n",
      "----------商品列表----------\n",
      "商品序号 (0)： 蜜雪柠檬水 6元\n",
      "商品序号 (1)： 蜜桃四季春 10元\n",
      "商品序号 (2)： 芒果奶盖 12元\n",
      "商品序号 (3)： 葡萄波波奶茶 8元\n",
      "请输入商品编号：2\n",
      "商品购买成功，正在制作中...您的余额还有13元\n",
      "----------商品列表----------\n",
      "商品序号 (0)： 蜜雪柠檬水 6元\n",
      "商品序号 (1)： 蜜桃四季春 10元\n",
      "商品序号 (2)： 芒果奶盖 12元\n",
      "商品序号 (3)： 葡萄波波奶茶 8元\n",
      "请输入商品编号：3\n",
      "商品购买成功，正在制作中...您的余额还有5元\n",
      "----------商品列表----------\n",
      "商品序号 (0)： 蜜雪柠檬水 6元\n",
      "商品序号 (1)： 蜜桃四季春 10元\n",
      "商品序号 (2)： 芒果奶盖 12元\n",
      "商品序号 (3)： 葡萄波波奶茶 8元\n",
      "请输入商品编号：3\n",
      "您的余额不足，请更换支付方式~~~\n",
      "----------商品列表----------\n",
      "商品序号 (0)： 蜜雪柠檬水 6元\n",
      "商品序号 (1)： 蜜桃四季春 10元\n",
      "商品序号 (2)： 芒果奶盖 12元\n",
      "商品序号 (3)： 葡萄波波奶茶 8元\n",
      "请输入商品编号：q\n",
      "您已购买的商品： [['葡萄波波奶茶', 8], ['芒果奶盖', 12], ['葡萄波波奶茶', 8]]\n",
      "您的余额还有5,欢迎下次光临~~\n"
     ]
    }
   ],
   "source": [
    "# 1. 运行程序后，让用户输入支付宝余额 ✔，然后打印我们的商品列表给用户。✔\n",
    "product_list = [\n",
    "    [\"蜜雪柠檬水\",6],\n",
    "    [\"蜜桃四季春\",10],\n",
    "    [\"芒果奶盖\",12],\n",
    "    [\"葡萄波波奶茶\",8]\n",
    "]\n",
    "shopping_car =[]\n",
    "AliPay = int(input(\"请输入余额：\"))\n",
    "while True:\n",
    "    print(\"----------商品列表----------\")\n",
    "    for i in range(4):\n",
    "        print(\"商品序号\",\"(\"+str(i)+\")：\",product_list[i][0],str(product_list[i][1])+\"元\")  # 格式化输出\n",
    "    # 2. 让用户输入商品编号进行商品的购买。:直接用商品列表的 index 作为商品标号\n",
    "    user_choice = input(\"请输入商品编号：\")\n",
    "    # 情景1：用户正在购买，输入商品编号\n",
    "    if user_choice.isdigit():\n",
    "        # 情景一：余额大于商品价格\n",
    "        if AliPay >= product_list[int(user_choice)][1]:\n",
    "            AliPay = AliPay - product_list[int(user_choice)][1]\n",
    "            shopping_car.append(product_list[int(user_choice)])   \n",
    "            print(\"商品购买成功，正在制作中...您的余额还有%s元\"%(AliPay))\n",
    "        # 情景二：余额小于商品价格\n",
    "        else:\n",
    "            print(\"您的余额不足，请更换支付方式~~~\")\n",
    "    # 情景2：用户输入退出，结束购买\n",
    "    elif user_choice == \"q\":\n",
    "        print(\"您已购买的商品：\",shopping_car)  \n",
    "        print(\"您的余额还有%s,欢迎下次光临~~\"%(AliPay)) \n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79e198d6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
